<?php


define('DEBUG', true);
require_once "common.php";

_write_html_frame('采集1688 迪士尼 产品数据');

if (version_compare(PHP_VERSION, '5.5.0', '<')) {
    _Log('require PHP > 5.5.0 !', 'error');
    die();
}

$base_domain = 'http://disneystore.anycps.com';


//自动登录后台系统
$ret = _request(
    $base_domain . '/sysadm/privilege.php',
    array(
        CURLOPT_REFERER => $base_domain . '/sysadm/privilege.php?act=login',
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => http_build_query(array(
            'username' => 'admin', # 登录帐号
            'password' => 'qimiao888', # 登录密码
            'remember' => 1, # 记住账号
            'act' => 'signin',
        ))
    )
);

_Log('开始采集1688 Disney 的数据');


#要采集的数据基础地址
$base_url = 'https://disney.1688.com/page/index.shtml';


_Log('开始采集分类数据！', 'note');
$homePage = _request($base_url);
phpQuery::newDocumentHTML($homePage);


//exit();
# 获取到所有的分类连接
$cats = pq('.subcate-item a');

$catCount = $cats->size();
if ($catCount > 0) {
    _Log('分类数据采集成功！', 'success');
    for ($i = 0; $i < $catCount; $i++) {
        $anchor = $cats->get($i);
        $catUrl = $anchor->getAttribute('href');
        $catName = trim($anchor->textContent);
        $catRefrere = $base_url;
        $catWhile = true;
        $page = 1;
        while ($catWhile) {
            # 获取到每一个分类的分页连接
            $catUrl = _set_url($catUrl, array(
                'query' => array(
                    'beginPage' => $page
                )
            ));

            # 开始采集分页数据
            _Log('开始采集分类：' . $catName . ' 第 ' . $page . ' 页数据', 'note');
            $listContent = _request($catUrl, $catRefrere);
            $catRefrere = $catUrl;
            $listContent = iconv('GBK', 'UTF-8', $listContent);
            $listContent = str_replace('GBK', 'UTF-8', $listContent);
            //exit($listContent);
            $trigger_content = $listContent;
            $trigger_while = 1;
            while ($trigger_while) {
                phpQuery::newDocumentHTML($trigger_content);
                $items = pq('li.sm-offer-item a.sm-offer-photoLink');


                # 阿里巴巴列表数据采用分段加载效果，在这里获取本页下一段数据地址
                $trigger = pq("div.sm-offer-trigger");
                if (1 == $trigger_while) {
                    if ($items->size() > 0) {
                        $catWhile = true;
                    } else {
                        $catWhile = false;
                    }
                }

                # 采集每一条数据信心
                getItems($items, $catUrl);

                if ($trigger->size() > 0) {
                    # 开始采集分段数据
                    $config = $trigger->get(0)->getAttribute('data-mod-config');
                    $config = json_decode($config, true);
                    $trigger_url = ($config['url']);
                    $trigger_url = str_replace('&amp;', '&', $trigger_url);
                    $callback = 'jQuery1495876254836745963281' ;//. NOW_TIME . rand();
                    $trigger_url = _set_url($trigger_url, array(
                        'query' => array(
                            'callback' => $callback,
                            //'_' => microtime(true)
                        )
                    ));

                    $trigger_ret = _request($trigger_url, $catUrl);
                    $trigger_ret = iconv('GBK', 'UTF-8', $trigger_ret);
                    $trigger_ret = str_replace('\\n', '', $trigger_ret);
                    $trigger_content = stripslashes($trigger_ret);
                    $trigger_while++;
                } else {
                    $trigger_while = false;
                }
            }


            if ($catWhile) {
                _Log('分类[' . $catName . ']第 ' . $page . ' 页数据采集完成！', 'success');
            } else {
                _Log('分类[' . $catName . ']第 ' . $page . ' 页数据采集失败！', 'error');
            }
            $page++;
        }

		
    }
	_Log('采集完成', 'error');
} else {
    _Log('分类数据采集失败！', 'error');
}

/**
 * @param phpQueryObject $pqEles
 */
function getItems($pqEles, $catUrl = '')
{
    global $base_domain;
    if ($pqEles && ($count = $pqEles->size()) > 0) {
        for ($i = 0; $i < $count; $i++) {
            $anchor = $pqEles->get($i);
            $itemUrl = $anchor->getAttribute('href');
            $itemName = trim($anchor->getAttribute('title'));
            //var_dump($itemName, $itemUrl);
            _Log('开始采集【' . $itemName . '】详细内容', 'note');
            $itemContent = _request($itemUrl, $catUrl);
            $itemContent = iconv('GBK', 'UTF-8', $itemContent);

            //图片列表
            phpQuery::newDocumentHTML($itemContent);
            $_images = pq('.tab-content .tab-trigger');
            $imgSize = $_images->size();
            $images = array();

            if ($imgSize > 0) {
                for ($j = 0; $j < $imgSize; $j++) {
                    $img = $_images->get($j)->getAttribute('data-imgs');
                    $images[] = json_decode($img, true);
                }
            }
            //'preview' => string 'https://cbu01.alicdn.com/img/ibank/2016/254/745/3266547452_874625441.400x400.jpg' (length=80)
            //'original' => string 'https://cbu01.alicdn.com/img/ibank/2016/254/745/3266547452_874625441.jpg'
            $goods_img = '';
            $little_img = '';
            $img_desc_files = array();
            $img_url_files = array();
            $img_file_files = array();
            foreach ($images as $key => &$image) {

                $image['preview600'] = collectImg(str_replace('.400x400.', '.600x600.', $image['preview']), $itemUrl);
                $image['preview'] = collectImg($image['preview'], $itemUrl);
                $image['original'] = collectImg($image['original'], $itemUrl);
                if ($key == '0') {
                    $goods_img = $image['preview'];
                    $little_img = $image['preview600'];

                }


                $img_file_files[] = '';
                $img_desc_files[] = '';
                $img_url_files[] = curl_file_create($image['preview600'], 'image/jpeg', basename($image['preview600']));;
                /*
                //var_dump(mime_content_type($image['preview600']));
                $ret = uploadFileToCloud($image['preview600']);
                if ($ret && 0 == $ret['code']) {
                    $img_file_files[] = $ret['url'];
                    $img_desc_files[] = '';
                    $img_url_files[] = '';//curl_file_create($image['preview600']);;
                }*/
            }


            //var_dump($images);
            //详细内容
            $detail_url = pq('#desc-lazyload-container')->get(0)->getAttribute('data-tfs-url');
            $detailContent = _request($detail_url, $itemUrl);
            $detailContent = iconv('GBK', 'UTF-8', $detailContent);
            $detailContent = str_replace('var offer_details=', '', $detailContent);
            $detailContent = str_replace('};', '}', $detailContent);
            $detailContent = json_decode($detailContent, true);
            if ($detailContent && isset($detailContent['content'])) {
                $detailContent = $detailContent['content'];
            } else {
                $detailContent = '';
            }

            //货号
            $de_feature = pq('.obj-content .de-feature');
            $de_value = pq('.obj-content .de-value');
            $de_size = $de_feature->size();
            //var_dump($itemUrl);
            $huohao = '';
            if (preg_match('/([0-9]+)\.html/', $itemUrl, $huohaos)) {
                $huohao = $huohaos[1];
            }
            $xmbh = '';
            for ($j = 0; $j < $de_size; $j++) {
                $feature = trim($de_feature->get($j)->textContent);
                //$detail_images[] = $src;
                if ('货号' == $feature) {
                    $huohao = trim($de_value->get($j)->textContent);
                }

                if ('项目编号' == $feature) {
                    $xmbh = trim($de_value->get($j)->textContent);
                }
            }
            if (!$huohao) $huohao = $xmbh;


            //价格
            $price = trim(pq('.price .value')->get(0)->textContent);


            //var_dump($detailContent);
            //exit($detailContent );
            //内容图片
            phpQuery::newDocumentHTML($detailContent);
            pq('a')->attr('href', '/');
            $_detail_images = pq('img');
            $_detail_imageSize = $_detail_images->size();
            $detail_images = array();

            phpQuery::newDocumentHTML('<div class="collect-content"></div>');
            if ($_detail_imageSize > 0) {
                for ($j = 0; $j < $_detail_imageSize; $j++) {
                    $src = $_detail_images->get($j)->getAttribute('src');
                    $detail_images[] = $src;
                    $file = collectImg($src, $itemUrl);
                    //上传数据到服务器
                    $cfile = uploadFileToCloud($file);
                    $url = $src;//'/themes/haohaios/images/loading.gif';
                    if ($cfile && 0 == $cfile['code']) {
                        $url = $cfile['url'];
                    }
                    $_detail_images->get($j)->setAttribute('src', $url);
                    pq('.collect-content')->append($_detail_images->get($j));
                    pq('.collect-content')->append('<br>');
                }
            }
            $detailContent = pq('')->html();

            /*echo _request('http://co.co.com/upfile.php', array(
                 CURLOPT_REFERER => $base_domain.'/sysadm/goods.php?act=list',
                 CURLOPT_POST => true,
                 CURLOPT_POSTFIELDS => array(
                     'password' => 'hashmap.cn',
                     'file' => curl_file_create($goods_img,'image/jpeg','goods_img')
                 )
             ),-1);exit();*/
            $_itemName = str_replace('迪士尼授权商品', '', iTrime($itemName));
            $_itemName = str_replace('官方授权商品', '', $_itemName);
            $_itemName = str_replace('迪士尼正品', '', $_itemName);
            $_itemName = str_replace('正品迪士尼', '', $_itemName);
            $post_data = array(
                'MAX_FILE_SIZE' => '2097152',
                'act' => 'insert',
                'goods_sn' => $huohao,
                'goods_name' => $_itemName,
                'cat_id' => '154',
                'shop_price' => $price,
                'brand_id' => 13,
                'market_price' => ceil($price * 1.5),
                'goods_img' => curl_file_create($goods_img, 'image/jpeg', basename($goods_img)),
                'give_integral' => -1,
                'bonus_allowed' => 0,
                'allow_fenxiao' => 0,
                'allow_sharej' => 0,
                'is_nearby' => 0,
                'team_num' => 5,
                'limit_buy_one' => 0,
                'discount_type' => 0,
                'is_miao' => 0,
                'is_luck' => 0,
                'subscribe' => 0,
                'little_img' => curl_file_create($little_img, 'image/jpeg', basename($little_img)),
                'goods_desc' => $detailContent,
                'goods_number' => 1000,
                'warn_number' => 1,
                'auto_thumb' => 1,
                'is_mall' => 1,
                /*
                'img_desc[]' => $img_desc_files,
                'img_url[]' => $img_url_files,
                'img_file[]' => $img_file_files,
                */
            );
            foreach ($img_url_files as $key => $img_url_file) {
                $post_data['img_desc[' . $key . ']'] = '';
                $post_data['img_url[' . $key . ']'] = $img_url_file;
                $post_data['img_file[' . $key . ']'] = '';
            }
            //http://disneystore.anycps.com/sysadm/goods.php
            //var_dump($post_data);exit();
            $ret = _request(
                $base_domain . '/sysadm/goods.php',
                array(
                    CURLOPT_REFERER => $base_domain . '/sysadm/goods.php?act=list',
                    CURLOPT_POST => true,
                    CURLOPT_SAFE_UPLOAD => true,
                    CURLOPT_POSTFIELDS => $post_data
                )
                , -1);

            phpQuery::newDocumentHTML($ret);
            pq('script')->remove();
            $ret = pq('td[style]:eq(0)')->text();
            if ('添加商品成功。' == $ret) {
                _Log($ret . '【' . $itemName . '】', 'success');
                saveItem($post_data['goods_name'], $huohao, $itemUrl);
            } else {
                _Log($ret . '【' . $itemName . '】', 'error');
            }
            //var_dump($post_data);

        }
    }
}

/**
 * @param $img
 * @param $referer
 *
 * @return bool|string
 */
function collectImg($img, $referer)
{
    $img_url = parse_url($img);
    $basename = basename($img_url['path']);
    $image_path = BASE_PATH . 'images/' . dirname($img_url['path']) . '/';
    if (!is_dir($image_path)) {
        mkdir($image_path, 0777, true);
    }
    $image_name = $image_path . $basename;
    $image_name = formatUrl($image_name);
    if (is_file($image_name)) {
        return $image_name;
    }
    $content = _request($img, $referer);
    $im = @imagecreatefromstring($content);
    //var_dump($im);
    if (imagejpeg($im, $image_name)) {
        return $image_name;
    }
    return false;
}

/**
 * 上传本地文件到服务器
 *
 * @param $file
 *
 * @return bool|mixed
 */
function uploadFileToCloud($file)
{
    global $base_domain;
    if (is_file($file)) {
        $ret = _request($base_domain . '/hmupfile.php', array(
            CURLOPT_REFERER => $base_domain . '/sysadm/privilege.php?act=login',
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => array(
                'username' => 'images/alibaba',
                'password' => 'hashmap.cn',
                'file' => curl_file_create($file),
            )
        ), -1);
        return json_decode($ret, true);
    }
    return false;
}


function saveItem($name, $ns, $link)
{
    static $head = false;
    $content = '';
    if (!$head) {
        $head = true;
        $content .= "商品\t货号\t详细连接\n";
    }
    $name = iTrime($name);
    $ns = iTrime($ns);
    $link = iTrime($link);
    $content .= "{$name}\t{$ns}\t{$link}\n";
    $fp = @fopen(getSaveFilename(), 'a');
    if ($fp) {
        $stuatus = @fwrite($fp, $content);
        @fclose($fp);
        if ($stuatus)
            return true;
    }
    return false;
}

function iTrime($string)
{
    $string = trim($string);
    $string = preg_replace('/\s*/', '', $string);
    return $string;
}


function getSaveFilename()
{
    $file_path = dirname(__FILE__) . '/data/';
    $file_name = 'disney_' . date('Y-m-d') . '_' . NOW_TIME . '.xls';
    if (!is_dir($file_path))
        mkdir($file_path, 0777, true);
    return $file_path . $file_name;
}